<template>
  <div>
    <el-table
      :data="tableData"
      border
      style="width: 100%"
      @selection-change="handleSelectChange"
      @sort-change="onSortChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column
        v-for="(item, index) in setting.column"
        :key="index"
        :prop="item.prop"
        :label="item.label"
        v-loading="loading"
        align="center"
        :sortable="item.sortable"
        :width="
          item.label === '意向' || item.label === '销售状态' ? '110' : 'auto'
        "
        :formatter="formatter"
      ></el-table-column>
      <el-table-column
        label="操作"
        v-show="setting.handles.length > 0"
        align="center"
        fixed="right"
        width="580"
      >
        <template slot-scope="scope">
          <el-button
            v-for="(item, index) in setting.handles"
            :key="index"
            :type="item.type"
            size="small"
            plain
            @click="handleClick(scope.$index, scope.row, item.handleClick)"
            >{{ item.name }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="bottom">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    setting: {
      type: Object,
      required: true,
    },
  },
  mounted() {
    console.log(this.setting);
  },
  data() {
    return {
      total: 0,
      tableData: [
        {
          name: "123",
        },
      ],
      loading: false,
      ps: 10,
      pn: 1,
      body: null,
    };
  },
  created() {
    if (this.setting.ajax) this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      this.body = this.setting.ajax.body;
      this.setting.ajax
        .handler({ ps: this.ps, pn: this.pn, ...this.setting.ajax.body })
        .then((response) => {
          if (response.code === 0) {
            this.tableData = response.data.records;
            this.total = response.data.total;
            this.loading = false;
            console.log(this.tableData, "tableData");
          }
        });
    },
    refresh(body = null) {
      this.loading = true;
      if (body) {
        this.setting.ajax
          .handler({ ps: this.ps, pn: this.pn, ...body })
          .then((response) => {
            if (response.code === 0) {
              this.tableData = response.data.records;
              this.total = response.data.total;
              this.loading = false;
            }
          });
      } else {
        this.setting.ajax
          .handler({ ps: this.ps, pn: this.pn, ...this.body })
          .then((response) => {
            if (response.code === 0) {
              this.tableData = response.data.records;
              this.total = response.data.total;
              this.loading = false;
            }
          });
      }
    },
    // 排序
    onSortChange(column, prop, order) {
      console.log("排序啦", column);
      let flag = 0;
      switch (column.order) {
        case "ascending":
          flag = 1;
          break;
        case "descending":
          flag = 2;
          break;
        default:
          flag = 0;
      }
      // this.tableData.sort(this.compare(prop,order));
      this.$emit("handleSortTable", { val: column.prop, flag });
    },
    handleSizeChange(val) {
      this.ps = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.pn = val;
      this.getList();
    },
    handleClick(index, row, name) {
      this.$emit("handleCommonClick", row, name);
    },
    handleSelectChange(val) {
      this.$emit("selectedValue", val);
    },
    formatter(row, column, cellValue, index) {
      if (column.label === "销售状态") {
        const arr = ["跟踪", "暂停", "关闭"];
        return arr[row.saleStatus];
      } else if (column.label === "意向") {
        const arr = ["低", "一般", "高"];
        return arr[row.intention];
      } else if (column.label == "项目创建时间") {
        if (row.createTime) {
          return row.createTime.replace(/T/g, " ");
        }
      } else if (column.label == "转正日期") {
        if (row.createTime) {
          return row.devTime.replace(/T/g, " ");
        }
      } else {
        return cellValue;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.bottom {
  margin-top: 20px;
}
</style>
